#include <iostream>
#include <sw/redis++/redis++.h>
#include <vector>
#include <thread>
#include <set>
#include "util.hpp"
using sw::redis::Redis;

void test1(Redis& redis)
{
    std::cout << "hset 和 hget" << std::endl;
    redis.flushall();
    redis.hset("key","key1","1");
    redis.hset("key","key2","2");
    redis.hset("key",std::make_pair("key3","3"));
    redis.hset("key",{std::make_pair("key4","4"),std::make_pair("key5","5")});
    std::vector<std::pair<std::string,std::string>> fields = {std::make_pair("key6","6"),std::make_pair("key7","7")};

    redis.hset("key",fields.begin(),fields.end());
    auto result = redis.hget("key","key7");
    if (result) 
    {
        std::cout << "result: " << result.value() << std::endl;
    } 
    else 
    {
        std::cout << "result 无效!" << std::endl;
    }
}
void test2(Redis& redis)
{
    std::cout << "hexits" << std::endl;
    redis.flushall();

    redis.hset("key", "f1", "111");
    redis.hset("key", "f2", "222");
    redis.hset("key", "f3", "333");

    bool result = redis.hexists("key", "f4");
    std::cout << "result: " << result << std::endl;
}
void test3(Redis& redis)
{
    std::cout << "hdel" << std::endl;
    redis.flushall();

    redis.hset("key", "f1", "111");
    redis.hset("key", "f2", "222");
    redis.hset("key", "f3", "333");

    long long result = redis.hdel("key", "f1");
    std::cout << "result: " << result << std::endl;

    result = redis.hdel("key", {"f2", "f3"});
    std::cout << "result: " << result << std::endl;

    long long len = redis.hlen("key");
    std::cout << "len: " << len << std::endl;
}
void test4(Redis& redis)
{
    std::cout << "hkeys 和 hvals" << std::endl;
    redis.flushall();
    redis.hset("key", "f1", "111");
    redis.hset("key", "f2", "222");
    redis.hset("key", "f3", "333");
    
    std::vector<std::string> fields;
    auto itfields = std::back_inserter(fields);
    redis.hkeys("key", itfields);
    PrintContainer(fields);

    std::vector<std::string> values;
    auto itvalues = std::back_inserter(values);
    redis.hvals("key", itvalues);
    PrintContainer(values);
}
void test5(Redis& redis)
{
    std::cout << "hmget 和 hmset" << std::endl;
    redis.flushall();

    redis.hmset("key", {
        std::make_pair("f1", "111"),
        std::make_pair("f2", "222"),
        std::make_pair("f3", "333")
    });
    std::vector<std::pair<std::string,std::string>> pairs = {
        std::make_pair("f4", "444"),
        std::make_pair("f5", "555"),
        std::make_pair("f6", "666")
    };
    redis.hmset("key", pairs.begin(), pairs.end());

    std::vector<std::string> values;
    auto it = std::back_inserter(values);
    redis.hmget("key", {"f1", "f4", "f3"}, it);
    PrintContainer(values);
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    // test1(redis);
    // test2(redis);
    // test3(redis);
    // test4(redis);
    test5(redis);
    
}